/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-10 16:51:31
 * @LastEditTime: 2022-12-05 11:15:08
 */
import {
  SET_SIDEBAR,
  SET_SELECTED_ITEM,
  ADD_REPORT,
  SET_ANNUALREP_LIST,
  EDIT_REPORT,
  DELETE_REPORT
} from '../constants';
import { delItemByIdArray, insertItem, updateItemById } from '@/utils/list';

export const AccountReducer = (state, action) => {
  switch (action.type) {
    case SET_SIDEBAR: {
      return { ...state, sidebarData: action.data };
    }
    /**
     * @description: 加载元数据列表
     * @param {Array} data 表格数据
     */
    case SET_ANNUALREP_LIST: {
      return {
        ...state,
        annualList: action.data
      };
    }
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };
    /* 新增列表 */
    case ADD_REPORT:
      return { ...state, annualList: insertItem(action.data, state.annualList) };
    /* 修改列表 */
    case EDIT_REPORT:
      return {
        ...state,
        annualList: updateItemById(action.data.id, action.data, state.annualList)
      };

    /**
     * @description: 删除元数据条目
     */
    case DELETE_REPORT: {
      return {
        ...state,
        annualList: delItemByIdArray(action.ids, state.annualList)
      };
    }
    default:
      console.log('untouched', action);
      return state;
  }
};
